Graphics.prototype.dump = function(){
  var s = "";
  var n = 0;
  for (var y=0;y<this.getHeight();y++) {
    s+="\n";
    for (var x=0;x<this.getWidth();x++) {
      var c = this.getPixel(x,y);
      s+=(c)?((c==1)?"#":"?"):" ";
    }
  }
  return s;
}
Graphics.prototype.print = function(){
  print("`"+this.dump()+"`");
}

var bits = [1,2,4,8,16,24];
result = 1;
bits.forEach(bpp => {
  console.log("Testing BPP ",bpp);
  var g = Graphics.createArrayBuffer(33,8,bpp,{msb:true});    
  if (E.getAddressOf(g.buffer)==0) throw new Error("Not a flat array! can't test properly");
  g.clearRect();
  g.setColor(1);
  var c = g.getColor();
  g.drawLine(0,0,32,32);
  for (var i=0;i<8;i++) g.fillRect(9+i,i, 32-i,i); // test fillRect at different offsets
//  if (bpp<=4) g.print();
  
  if (bpp==1 && btoa(g.buffer)!="gH///6Af//+IB///ggH//4CAf/+AIB//gAgH/4ACAf+A") { result = 0; print("Data stored wrong"); }
  if (bpp==2 && btoa(g.buffer)!="QAAVVVVVVVVEAAFVVVVVVUBAABVVVVVVQAQAAVVVVVVAAEAAFVVVVUAABAABVVVVQAAAQAAVVVVAAAAEAAFVVUAA") { result = 0; print("Data stored wrong"); }
  if (bpp==4 && btoa(g.buffer)!="EAAAAAERERERERERERERERAQAAAAAREREREREREREREQABAAAAABEREREREREREREAAAEAAAAAERERERERERERAAAAAQAAAAAREREREREREQAAAAABAAAAABEREREREREAAAAAAAEAAAAAERERERERAAAAAAAAAQAAAAAREREREQAAAA") { result = 0; print("Data stored wrong"); }
  if (bpp==8 && btoa(g.buffer)!="AQAAAAAAAAAAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAAEAAAAAAAAAAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEAAAABAAAAAAAAAAABAQEBAQEBAQEBAQEBAQEBAQEBAQAAAAAAAQAAAAAAAAAAAQEBAQEBAQEBAQEBAQEBAQEBAAAAAAAAAAEAAAAAAAAAAAEBAQEBAQEBAQEBAQEBAQEAAAAAAAAAAAABAAAAAAAAAAABAQEBAQEBAQEBAQEBAQAAAAAAAAAAAAAAAQAAAAAAAAAAAQEBAQEBAQEBAQEBAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAEBAQEBAQEBAQEAAAAAAAAA") { result = 0; print("Data stored wrong"); }
  if (bpp==16 && btoa(g.buffer)!="AAEAAAAAAAAAAAAAAAAAAAAAAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAAAAQAAAAAAAAAAAAAAAAAAAAAAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAQAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAABAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAEAAQABAAEAAQABAAEAAQABAAEAAQABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAQABAAEAAQABAAEAAQABAAEAAQAAAAAAAAAAAAAAAAAA") { result = 0; print("Data stored wrong"); }
  if (bpp==24 && btoa(g.buffer)!="AAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAABAAABAAABAAABAAABAAABAAABAAABAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAA") { result = 0; print("Data stored wrong"); }
  //print(`  if (bpp==${bpp} && btoa(g.buffer)!=${E.toJS(btoa(g.buffer))}) { result = 0; print("Data stored wrong"); }`);  
  
  
  // test the line reports as being in the right place
  for (var y=0;y<8;y++)
    for (var x=0;x<8;x++) {
      var p = g.getPixel(x,y), exp = ((x==y)?c:0);
      if (p!=exp) {
        print(`Wrong pixel at ${x},${y} (${p} != ${exp})`);
        result = 0;
      }
    }
  
});
